package com.cskaoyan.realm;

import com.cskaoyan.bean.ActiveUser;
import com.cskaoyan.bean.user.WxUserLoginData;
import com.cskaoyan.mapper.AdminMapper;
import com.cskaoyan.mapper.WxUserMapper;
import com.cskaoyan.vo.user.WxUserLoginVo;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class WxRealm extends AuthorizingRealm {

    // TODO：改为usermapper
    @Autowired
    WxUserMapper wxUserMapper;

    // 认证：en
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();

        WxUserLoginData wxUserLoginData= wxUserMapper.selectInfoByUsername(username);
        // String pswFromDB = wxUserMapper.selectPswByUsername(username);

        // 第三个参数是realm的名字，不重要
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(wxUserLoginData, wxUserLoginData.getPassword(), this.getName());
        return authenticationInfo;
    }

    // 授权：or
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        // ActiveUser activeUser = (ActiveUser) principalCollection.getPrimaryPrincipal();
        // // TODO：去数据库中查出permissions
        // List<String> permissions= new ArrayList<>();
        // permissions.add("*");
        // // permissions.add("admin:admin:list");
        // SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        // authorizationInfo.addStringPermissions(activeUser.getPerms());
        return null;
    }


}
